﻿package commands 
{
	import flash.utils.ByteArray;
	import commands.classesinfo.*;
	
	/**
	 * ...
	 * @author wwx
	 * 50000009 玩家列表
	 */
	public class PlayerListCmd extends abCommand
	{
		
		public function PlayerListCmd() 
		{
			super(CommandType(CommandType.CMD_TYPE_PLAYERLIST));
			playersInfo = new Array();
		}
		
		public var msgSeq:int;//消息号
		public var errorNum:int;//错误号
		
		public var roomId:int;//
		public var playersInfo:Array;// PlayerInfoInList;
		
		public function convertToCmd(bytes:ByteArray, offset:int, len:int):int
		{
			var ret:int = -1;
			msgSeq = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
			offset += 4;
			errorNum = (int) (bytes[offset+0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			
			roomId = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
			offset += 4;
			ret = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
			offset += 4;
			
			var playerInfo:PlayerInfoInList = null;
			//playersInfo = new Array();
			var strLen:int = 0;
			for (var i:int = 0; i < ret; i++)
			{
				playerInfo = new PlayerInfoInList();
				playerInfo.playerId = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
				offset += 4;
				playerInfo.playerSex = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
				offset += 4;
				
				strLen = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8);
				offset += 2;
				
				bytes.position = offset;
				playerInfo.playerName = bytes.readMultiByte(strLen, "gb2312");
				offset += strLen;
				
				playerInfo.hadChips = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
				offset += 4;
				
				playersInfo[i] = playerInfo;
			}
			return 0;
		}
	}
	
}